package com.corpgovernment.organization.service;

import com.corpgovernment.api.operatelog.chooseconstant.ChooseBusinesstype;
import com.corpgovernment.api.operatelog.soa.OperateLogClient;
import com.corpgovernment.api.operatelog.soa.request.AddOperateLogRequestVo;
import com.corpgovernment.api.operatelog.util.OperateLogContentUtil;
import com.corpgovernment.api.operatelog.vo.OperateLogContentDataVo;
import com.corpgovernment.common.base.AbstractBaseService;
import com.corpgovernment.common.base.BaseUserInfo;
import com.corpgovernment.common.entity.db.BaseEntity;
import com.corpgovernment.common.utils.ExecutorUtil;
import com.corpgovernment.organization.entity.db.MbPostInfo;
import com.corpgovernment.redis.cache.UserCacheManager;
import com.ctrip.corp.obt.generic.core.context.UserInfoContext;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class OrganizationBaseService extends AbstractBaseService {
    @Autowired
    private OperateLogClient operateLogService;
    @Autowired
    private UserCacheManager userCacheManager;

    protected void insertOperateLog(BaseEntity baseEntity) {
        try {
            MbPostInfo mbPostInfo = JsonUtils.convert(baseEntity, MbPostInfo.class);

            //BaseUserInfo baseUserInfo = userCacheManager.getBaseUserInfo();
            BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
            ExecutorUtil.logExecutor.execute(() -> {
                if (baseUserInfo != null) {
                    AddOperateLogRequestVo addOperateLogRequestVo = new AddOperateLogRequestVo();
                    String businessId = String.valueOf(baseEntity.getId());
                    try {
                        List<OperateLogContentDataVo> operateLogContentDataVos = OperateLogContentUtil.toContent(null, baseEntity);
                        if(!CollectionUtils.isEmpty(operateLogContentDataVos)){
                            for (OperateLogContentDataVo contentDataVo:operateLogContentDataVos){
                                if("删除状态".equals(contentDataVo.getField())){
                                    if (mbPostInfo != null && !mbPostInfo.getIsDeleted()){
                                        contentDataVo.setNewValue("否");
                                    }
                                }
                            }
                        }
                        operateLogService.addOperateLog(OperateLogContentUtil.toAddOperateLog(baseUserInfo, businessId, addOperateLogRequestVo,
                                operateLogContentDataVos, ChooseBusinesstype.CONFIGPOSITION));
                    } catch (Exception ex) {
                        log.error("organizationManageLog",
                                String.format("新增操作日志错误：[%s][%s]", JsonUtils.toJsonString(addOperateLogRequestVo), ex.getMessage()), null);
                    }
                }
            });
        } catch (Exception ex) {
            log.error("organizationManageLog",
                    String.format("新增操作日志线程错误：[%s]", ex.getMessage()), null);
        }
    }

    public void insertLog(BaseEntity baseEntity,String type) {
        try {
            //BaseUserInfo baseUserInfo = userCacheManager.getBaseUserInfo();
            BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
            ExecutorUtil.logExecutor.execute(() -> {
//                if (baseUserInfo != null) {
                    AddOperateLogRequestVo addOperateLogRequestVo = new AddOperateLogRequestVo();
                    String businessId = String.valueOf(baseEntity.getId());
                    try {
                        List<OperateLogContentDataVo> operateLogContentDataVos = OperateLogContentUtil.toContent(null, baseEntity);
                        operateLogService.addOperateLog(OperateLogContentUtil.toAddOperateLog(baseUserInfo, businessId, addOperateLogRequestVo,
                                operateLogContentDataVos, type));
                    } catch (Exception ex) {
                        log.error("organizationManageLog",
                                String.format("新增[%s]操作日志错误：[%s][%s]",type, JsonUtils.toJsonString(addOperateLogRequestVo), ex.getMessage()));
                    }
//                }
            });
        } catch (Exception ex) {
            log.error("organizationManageLog",
                    String.format("新增[%s]操作日志线程错误：[%s]", type,ex.getMessage()));
        }
    }

    public void updateOperateLog(String id, List<OperateLogContentDataVo> dataVo) {
        try {
            //BaseUserInfo baseUserInfo = userCacheManager.getBaseUserInfo();
            BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
            ExecutorUtil.logExecutor.execute(() -> {
                if (baseUserInfo != null) {
                    AddOperateLogRequestVo addOperateLogRequestVo = new AddOperateLogRequestVo();
                    try {
                        List<OperateLogContentDataVo> operateLogContentDataVos = Lists.newArrayList(dataVo);
                        operateLogService.addOperateLog(OperateLogContentUtil.toAddOperateLog(baseUserInfo, id, addOperateLogRequestVo,
                                operateLogContentDataVos, ChooseBusinesstype.CONFIGPOSITION));
                    } catch (Exception ex) {
                        log.error("organizationManageLog",
                                String.format("新增操作日志错误：[%s][%s]", JsonUtils.toJsonString(addOperateLogRequestVo), ex.getMessage()), null);
                    }
                }
            });
        } catch (Exception ex) {
            log.error("organizationManageLog", String.format("新增操作日志线程错误：[%s]", ex.getMessage()), null);
        }
    }

    public void updateLog(BaseEntity oldValue, BaseEntity newValue,String type) {
        try {
//            MbPostInfo mbPostInfo = JsonUtils.convert(oldValue, MbPostInfo.class);
            //BaseUserInfo baseUserInfo = userCacheManager.getBaseUserInfo();
            BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
            ExecutorUtil.logExecutor.execute(() -> {
//                if (baseUserInfo != null) {
                    AddOperateLogRequestVo addOperateLogRequestVo = new AddOperateLogRequestVo();
                    String businessId = String.valueOf(oldValue.getId());
                    try {
                        List<OperateLogContentDataVo> operateLogContentDataVos = OperateLogContentUtil.toContent(oldValue, newValue);
//                        if(!CollectionUtils.isEmpty(operateLogContentDataVos)){
//                            for (OperateLogContentDataVo contentDataVo:operateLogContentDataVos){
//                                if("删除状态".equals(contentDataVo.getField())){
//                                    if (mbPostInfo != null && !mbPostInfo.isDeleted()){
//                                        contentDataVo.setNewValue("否");
//                                    }
//                                }
//                            }
//                        }

                        operateLogService.addOperateLog(OperateLogContentUtil.toAddOperateLog(baseUserInfo, businessId, addOperateLogRequestVo,
                                operateLogContentDataVos, type));
                    } catch (Exception ex) {
                        log.error("organizationManageLog",
                                String.format("修改操作日志错误：[%s][%s]", JsonUtils.toJsonString(addOperateLogRequestVo), ex.getMessage()));
                    }
//                }
            });
        } catch (Exception ex) {
            log.error("organizationManageLog",
                    String.format("修改操作日志线程错误：[%s]", ex.getMessage()));
        }
    }

    protected void updateOperateLog(BaseEntity oldValue, BaseEntity newValue) {
        try {
            MbPostInfo mbPostInfo = JsonUtils.convert(oldValue, MbPostInfo.class);
            //BaseUserInfo baseUserInfo = userCacheManager.getBaseUserInfo();
            BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
            ExecutorUtil.logExecutor.execute(() -> {
                if (baseUserInfo != null) {
                    AddOperateLogRequestVo addOperateLogRequestVo = new AddOperateLogRequestVo();
                    String businessId = Optional.ofNullable(oldValue).map( i -> {return String.valueOf(oldValue.getId());}).orElse("") ;
                    try {
                        List<OperateLogContentDataVo> operateLogContentDataVos = OperateLogContentUtil.toContent(oldValue, newValue);
                        if(!CollectionUtils.isEmpty(operateLogContentDataVos)){
                            for (OperateLogContentDataVo contentDataVo:operateLogContentDataVos){
                                if("删除状态".equals(contentDataVo.getField())){
                                    if (mbPostInfo != null && !mbPostInfo.getIsDeleted()){
                                        contentDataVo.setNewValue("否");
                                    }
                                }
                            }
                        }

                        operateLogService.addOperateLog(OperateLogContentUtil.toAddOperateLog(baseUserInfo, businessId, addOperateLogRequestVo,
                                operateLogContentDataVos, ChooseBusinesstype.CONFIGPOSITION));
                    } catch (Exception ex) {
                        log.error("organizationManageLog",
                                String.format("修改职级管理操作日志错误：[%s][%s]", JsonUtils.toJsonString(addOperateLogRequestVo), ex.getMessage()), null);
                    }
                }
            });
        } catch (Exception ex) {
            log.error("organizationManageLog",
                    String.format("修改职级管理操作日志线程错误：[%s]", ex.getMessage()), null);
        }
    }

    protected void deleteOperateLog(String id) {
        try {
            //BaseUserInfo baseUserInfo = userCacheManager.getBaseUserInfo();
            BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
            ExecutorUtil.logExecutor.execute(() -> {
                if (baseUserInfo != null) {
                    AddOperateLogRequestVo addOperateLogRequestVo = new AddOperateLogRequestVo();
                    try {
                        List<OperateLogContentDataVo> operateLogContentDataVos = Lists.newArrayList(OperateLogContentDataVo.logicDelete());
                        operateLogService.addOperateLog(OperateLogContentUtil.toAddOperateLog(baseUserInfo, id, addOperateLogRequestVo,
                                operateLogContentDataVos, ChooseBusinesstype.CONFIGPOSITION));
                    } catch (Exception ex) {
                        log.error("organizationManageLog",
                                String.format("新增员工用户信息日志错误：[%s][%s]", JsonUtils.toJsonString(addOperateLogRequestVo), ex.getMessage()), null);
                    }
                }
            });
        } catch (Exception ex) {
            log.error("organizationManageLog",
                    String.format("新增员工用户信息日志线程错误：[%s]", ex.getMessage()), null);
        }
    }


    public void deleteLog(BaseEntity oldValue, BaseEntity newValue,String type) {
        try {
            //BaseUserInfo baseUserInfo = userCacheManager.getBaseUserInfo();
            BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
//            MbPostInfo mbPostInfo = JsonUtils.convert(oldValue, MbPostInfo.class);
            log.info("baseUserInfo:{}，oldValue:{},newValue:{}", baseUserInfo, oldValue, newValue);
            ExecutorUtil.logExecutor.execute(() -> {
//                if (baseUserInfo != null) {
                    AddOperateLogRequestVo addOperateLogRequestVo = new AddOperateLogRequestVo();
                    String businessId = String.valueOf(oldValue.getId());
                    try {
                        List<OperateLogContentDataVo> operateLogContentDataVos = OperateLogContentUtil.deleteToContent(oldValue, newValue);
//                        if(!CollectionUtils.isEmpty(operateLogContentDataVos)){
//                            for (OperateLogContentDataVo contentDataVo:operateLogContentDataVos) {
//                                if("删除状态".equals(contentDataVo.getField())){
//                                    if(mbPostInfo != null && !mbPostInfo.isDeleted()){
//                                        contentDataVo.setNewValue("是");
//                                        contentDataVo.setOldValue("否");
//                                    }
//                                }
//                            }
//                        }
                        operateLogService.addOperateLog(OperateLogContentUtil.toAddOperateLog(baseUserInfo, businessId, addOperateLogRequestVo,
                                operateLogContentDataVos, type));
                    } catch (Exception ex) {
                        log.error(String.format("删除操作日志错误：[%s][%s]", JsonUtils.toJsonString(addOperateLogRequestVo), ex.getMessage()));
                    }
//                }
            });
        } catch (Exception ex) {
            log.error(String.format("删除操作日志线程错误：[%s]", ex.getMessage()));
        }
    }

    protected void deleteOperateLog(String id, List<OperateLogContentDataVo> dataVo) {
        try {
            //BaseUserInfo baseUserInfo = userCacheManager.getBaseUserInfo();
            BaseUserInfo baseUserInfo = UserInfoContext.getContextParams(BaseUserInfo.class);
            ExecutorUtil.logExecutor.execute(() -> {
                if (baseUserInfo != null) {
                    AddOperateLogRequestVo addOperateLogRequestVo = new AddOperateLogRequestVo();
                    try {
                        List<OperateLogContentDataVo> operateLogContentDataVos = Lists.newArrayList(dataVo);
                        operateLogService.addOperateLog(OperateLogContentUtil.toAddOperateLog(baseUserInfo, id, addOperateLogRequestVo,
                                operateLogContentDataVos, ChooseBusinesstype.CONFIGPOSITION));
                    } catch (Exception ex) {
                        log.error("organizationManageLog",
                                String.format("删除操作日志错误：[%s][%s]", JsonUtils.toJsonString(addOperateLogRequestVo), ex.getMessage()), null);
                    }
                }
            });
        } catch (Exception ex) {
            log.error("organizationManageLog", String.format("删除操作日志线程错误：[%s]", ex.getMessage()), null);
        }
    }

}
